﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using Telerik.Web.UI;
using Telerik.Charting;
using System.Drawing;

namespace ARR.Services.Domain.Reports
{
    public static class ReportsHelper
    {
        public static BaseFont GetCalibriOrDefault()
        {
            try
            {
                BaseFont calibri = BaseFont.CreateFont(@"C:\Windows\Fonts\calibri.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
                return calibri;
            }
            catch
            {
                return BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            }
        }

        public static RadChart GetPieChart(string chartTitle)
        {
            RadChart chart = new RadChart();
            chart.Width = 250;
            chart.Height = 250;
            chart.AutoLayout = true;
            chart.Appearance.ImageQuality = Telerik.Charting.Styles.ImageQuality.HighQuality;
            //chart.Appearance.TextQuality = Telerik.Charting.Styles.TextQuality.SingleBitPerPixelGridFit;
            chart.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
            chart.Appearance.FillStyle.MainColor = System.Drawing.Color.White;
            chart.ChartTitle.TextBlock.Text = chartTitle;
            chart.ChartTitle.Appearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Top;
            chart.ChartTitle.TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("calibri", 10f, System.Drawing.FontStyle.Bold);
            chart.PlotArea.Appearance.Dimensions.Margins = new Telerik.Charting.Styles.ChartMargins(30, 0, 0, 0);
            chart.PlotArea.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
            chart.PlotArea.Appearance.FillStyle.MainColor = System.Drawing.Color.White;
            chart.PlotArea.Appearance.Border.Visible = false;
            chart.Appearance.Border.Visible = false;
            chart.Legend.Visible = true;
            chart.Legend.Appearance.Location = Telerik.Charting.Styles.LabelLocation.OutsidePlotArea;
            chart.Legend.Appearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Bottom;
            chart.Legend.Appearance.Overflow = Telerik.Charting.Styles.Overflow.Row;
            chart.Legend.Appearance.Border.Visible = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Figure = "square";
            chart.Legend.Appearance.ItemMarkerAppearance.Border.Visible = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Dimensions.AutoSize = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Dimensions.SetDimensions(5, 5);
            chart.Legend.Appearance.ItemTextAppearance.TextProperties.Font = new System.Drawing.Font("calibri", 8f, System.Drawing.FontStyle.Regular);

            if (string.IsNullOrWhiteSpace(chartTitle))
            {
                chart.ChartTitle.Visible = false;
            }

            return chart;
        }

        public static RadChart GetBarsChart(string chartTitle)
        {
            RadChart chart = new RadChart();
            chart.Width = 550;
            chart.Height = 290;
            chart.AutoLayout = true;            
            chart.Appearance.ImageQuality = Telerik.Charting.Styles.ImageQuality.HighQuality;
            //chart.Appearance.TextQuality = Telerik.Charting.Styles.TextQuality.SingleBitPerPixelGridFit;
            chart.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
            chart.Appearance.FillStyle.MainColor = System.Drawing.Color.White;
            chart.Appearance.BarWidthPercent = 50;
            chart.Appearance.BarOverlapPercent = -10;
            chart.ChartTitle.TextBlock.Text = chartTitle;
            chart.ChartTitle.Appearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Top;
            chart.ChartTitle.TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("Calibri", 9f, System.Drawing.FontStyle.Bold);
            chart.PlotArea.Appearance.Dimensions.Margins = new Telerik.Charting.Styles.ChartMargins(30, 0, 0, 0);
            chart.PlotArea.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
            chart.PlotArea.Appearance.FillStyle.MainColor = System.Drawing.Color.White;
            chart.PlotArea.Appearance.Border.Visible = false;
            chart.PlotArea.YAxis.Visible = Telerik.Charting.Styles.ChartAxisVisibility.False;
            chart.PlotArea.XAxis.AutoScale = false;
            chart.PlotArea.XAxis.Appearance.TextAppearance.AutoTextWrap = Telerik.Charting.Styles.AutoTextWrap.True;
            chart.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Font = new System.Drawing.Font("Calibri", 8f, System.Drawing.FontStyle.Regular);
            chart.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Color = System.Drawing.Color.Black;
            chart.Appearance.Border.Visible = false;
            chart.Appearance.Border.Visible = false;
            chart.Legend.Visible = true;
            chart.Legend.Appearance.Location = Telerik.Charting.Styles.LabelLocation.OutsidePlotArea;
            chart.Legend.Appearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Bottom;
            chart.Legend.Appearance.Overflow = Telerik.Charting.Styles.Overflow.Row;
            chart.Legend.Appearance.Border.Visible = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Figure = "square";
            chart.Legend.Appearance.ItemMarkerAppearance.Border.Visible = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Dimensions.AutoSize = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Dimensions.SetDimensions(5, 5);
            chart.Legend.Appearance.ItemTextAppearance.TextProperties.Font = new System.Drawing.Font("Calibri", 8f, System.Drawing.FontStyle.Regular);

            if (string.IsNullOrWhiteSpace(chartTitle))
            {
                chart.ChartTitle.Visible = false;
            }

            return chart;
        }
        
        public static RadChart GetLineChart(string chartTitle, string XAxisLabel, string YAxisLabel)
        {
            RadChart chart = new RadChart();
            chart.Width = 550;
            chart.Height = 300;
            chart.AutoLayout = true;
            chart.Appearance.ImageQuality = Telerik.Charting.Styles.ImageQuality.HighQuality;
            chart.Appearance.TextQuality = Telerik.Charting.Styles.TextQuality.SingleBitPerPixelGridFit;            
            chart.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
            chart.Appearance.FillStyle.MainColor = System.Drawing.Color.White;
            chart.Appearance.BarWidthPercent = 60;
            chart.Appearance.BarOverlapPercent = -10;
            chart.Appearance.Border.Visible = false;
            chart.ChartTitle.TextBlock.Text = chartTitle;
            chart.ChartTitle.Appearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Top;
            chart.ChartTitle.TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("Calibri", 11f, System.Drawing.FontStyle.Bold);
            chart.PlotArea.Appearance.Dimensions.Margins = new Telerik.Charting.Styles.ChartMargins(30, 0, 0, 0);
            chart.PlotArea.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
            chart.PlotArea.Appearance.FillStyle.MainColor = System.Drawing.Color.White;
            chart.PlotArea.Appearance.Border.Visible = false;
            chart.PlotArea.YAxis.Appearance.TextAppearance.TextProperties.Font = new System.Drawing.Font("Calibri", 7f, System.Drawing.FontStyle.Regular);
            chart.PlotArea.YAxis.Appearance.TextAppearance.TextProperties.Color = System.Drawing.Color.Black;
            chart.PlotArea.YAxis.Visible = Telerik.Charting.Styles.ChartAxisVisibility.True;
            chart.PlotArea.YAxis.Appearance.MajorGridLines.Visible = true;
            chart.PlotArea.YAxis.Appearance.MajorGridLines.Color = System.Drawing.Color.LightGray;
            chart.PlotArea.YAxis.Appearance.MajorGridLines.Width = 1f;
            chart.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Font = new System.Drawing.Font("Calibri", 7f, System.Drawing.FontStyle.Regular);
            chart.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Color = System.Drawing.Color.Black;
            chart.PlotArea.XAxis.LayoutMode = Telerik.Charting.Styles.ChartAxisLayoutMode.Normal;
            chart.Legend.Visible = true;
            chart.Legend.Appearance.Location = Telerik.Charting.Styles.LabelLocation.OutsidePlotArea;
            chart.Legend.Appearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Bottom;
            chart.Legend.Appearance.Overflow = Telerik.Charting.Styles.Overflow.Row;
            chart.Legend.Appearance.Border.Visible = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Figure = "square";
            chart.Legend.Appearance.ItemMarkerAppearance.Border.Visible = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Dimensions.AutoSize = false;
            chart.Legend.Appearance.ItemMarkerAppearance.Dimensions.SetDimensions(5, 5);
            chart.Legend.Appearance.ItemTextAppearance.TextProperties.Font = new System.Drawing.Font("Calibri", 7f, System.Drawing.FontStyle.Regular);

            if (!string.IsNullOrWhiteSpace(XAxisLabel))
            {
                chart.PlotArea.XAxis.AxisLabel.TextBlock.Text = XAxisLabel;
                chart.PlotArea.XAxis.AxisLabel.Appearance.Visible = true;
                chart.PlotArea.XAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Black;
                chart.PlotArea.XAxis.AxisLabel.TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("calibri", 9f, System.Drawing.FontStyle.Regular);
            }

            if (!string.IsNullOrWhiteSpace(YAxisLabel))
            {
                chart.PlotArea.YAxis.AxisLabel.TextBlock.Text = YAxisLabel;
                chart.PlotArea.YAxis.AxisLabel.Appearance.Visible = true;
                chart.PlotArea.YAxis.AxisLabel.Appearance.RotationAngle = 270;
                chart.PlotArea.YAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Black;
                chart.PlotArea.YAxis.AxisLabel.TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("calibri", 9f, System.Drawing.FontStyle.Regular);
            }

            if (string.IsNullOrWhiteSpace(chartTitle))
            {
                chart.ChartTitle.Visible = false;
            }

            return chart;
        }

        public static ChartSeriesItem GetPieSeriesItem(string name, Color itemColor)
        {
            ChartSeriesItem serieItem = new ChartSeriesItem();
            serieItem.Name = name;

            serieItem.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.ComplexGradient;
            serieItem.Appearance.FillStyle.FillSettings.ComplexGradient.Add(new GradientElement(System.Drawing.Color.FromArgb(180, itemColor), 0f));
            serieItem.Appearance.FillStyle.FillSettings.ComplexGradient.Add(new GradientElement(itemColor, 0.9f));
            serieItem.Appearance.FillStyle.FillSettings.ComplexGradient.Add(new GradientElement(itemColor, 1f));
            serieItem.Appearance.FillStyle.FillSettings.GradientMode = Telerik.Charting.Styles.GradientFillStyle.Circle;

            serieItem.Appearance.Border.Color = System.Drawing.Color.White;            
            serieItem.Label.Appearance.LabelLocation = Telerik.Charting.Styles.StyleSeriesItemLabel.ItemLabelLocation.Inside;
            serieItem.Label.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.White;
            serieItem.Label.TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("calibri", 9f);

            return serieItem;
        }

        public static ChartSeriesItem GetBarsSeriesItem(string name)
        {
            ChartSeriesItem serieItem = new ChartSeriesItem();
            serieItem.Name = name;
            serieItem.Label.Appearance.LabelLocation = Telerik.Charting.Styles.StyleSeriesItemLabel.ItemLabelLocation.Outside;
            serieItem.Label.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Black;
            serieItem.Label.TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("calibri", 9f);

            return serieItem;
        }
    }
}
